package stackqueue;

import java.util.Arrays;
import java.util.Stack;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/17 16:31
 * https://leetcode-cn.com/problems/next-greater-element-ii/
 */
public class NextGreaterElementsII {

    public int[] nextGreaterElements(int[] nums) {
        int[] result = new int[nums.length];
        Arrays.fill(result,-1);
        Stack<Integer> stack = new Stack<>();
        stack.push(0);
        int n = nums.length;
        for (int i = 0; i < result.length; i++) {
            while (!stack.isEmpty() && nums[i % n] > nums[stack.peek()]) {
                Integer pop = stack.pop();
                result[pop] = nums[i % n];
            }
            stack.push(i % n);
        }
        return Arrays.copyOfRange(result, 0, n);
    }

    public static void main(String[] args) {

    }
}
